import { createStore } from 'vuex'
import router from '@/router'
import { setTime } from '@/utils/constant'

export default createStore({
  state: {
    token: localStorage.getItem('token') || '',
    ToggleSide: false // 收缩栏默认值
  },
  getters: {
  },
  mutations: {
    // 设置token
    setToken (state, token) {
      state.token = token
      localStorage.setItem('token', token)
    },
    // 收缩栏默认值的改变
    setToggleSide (state) {
      state.ToggleSide = !state.ToggleSide
    }
  },
  actions: {
    // 登录
    login ({ commit }, info) {
      // 存入登录的时间
      setTime()
      commit('setToken', info)
      router.replace('/')
    },
    // 退出登录
    loginOut ({ commit }) {
      commit('setToken', '')
      localStorage.removeItem('token')
      router.replace('/login')
    }
  },
  modules: {
  }
})
